package cn.jluzh.blog.modules.security.controller;


import cn.jluzh.blog.commons.result.Result;
import cn.jluzh.blog.commons.result.ResultCode;
import cn.jluzh.blog.commons.result.ResultFactory;
import cn.jluzh.blog.modules.log.aspect.annotation.AutoLog;
import cn.jluzh.blog.modules.security.eneity.SysUserToken;
import cn.jluzh.blog.modules.security.service.impl.SysUserTokenServiceImpl;
import cn.jluzh.blog.modules.user.entity.SysUser;
import cn.jluzh.blog.modules.user.service.impl.UserServiceImpl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Date;


/**
 * @author hzhii
 * 登录Controller接口
 * */
@RestController
@Api(tags = "登录相关接口")
public class    LoginController {

    @Autowired
    UserServiceImpl userService;

    @Autowired
    SysUserTokenServiceImpl sysUserTokenService;



    /**
     * 账号注册
     */
    @PostMapping("/register")
    @ApiOperation("注册接口")
    @AutoLog("注册操作")
    public Result register(@RequestBody JSONObject json) throws IOException {
        String account = json.getString("account");
        String password = json.getString("password");
        String nickname = json.getString("nickname");
        if (StringUtils.isBlank(account) || StringUtils.isBlank(password) || StringUtils.isBlank(nickname)) {
            return ResultFactory.buildFailResult("参数不对");
        }


        //用户信息
        SysUser user = userService.getUserByAccount(account);

        //账号已存在
        if (user != null) {
            return ResultFactory.buildFailResult("该账号已存在");
        }

        user = new SysUser();
        user.setAccount(account);
        user.setPassword(password);
        user.setAdmin(1);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        user.setNickname(nickname);
        user.setLastLoginTime(new Date());
        user.setStatus("1");
        userService.save(user);
        return ResultFactory.buildSuccessResult(true);
    }

    @PostMapping("login")
    @ApiOperation("登录接口")
    public Result login(@RequestBody SysUser user) {
        if (StringUtils.isBlank(user.getAccount()) || StringUtils.isBlank(user.getPassword())) {
            return ResultFactory.buildFailResult("参数不对");
        }
        // 获取用户信息
        SysUser sysUser = userService.getUserByAccount(user.getAccount());

        if(sysUser ==null || !sysUser.getPassword().equals(user.getPassword())){
            return ResultFactory.buildResult(ResultCode.SUCCESS,"账号或密码错误",null);
        }else{
            return sysUserTokenService.createToken(sysUser.getId());
        }
    }

    @PostMapping("logout")
    @ApiOperation("登出接口")
    @AutoLog("登出操作")
    public Result logout(@RequestHeader("Oauth-Token")String token){
        //获取当前登陆了的用户的token
        SysUserToken userToken = sysUserTokenService.getByToken(token);
        //退出用户更新数据库后台token
        sysUserTokenService.logout(userToken);

        return ResultFactory.buildResult(ResultCode.SUCCESS,"退出成功!",null);
    }
}
